package com.market.controller.admin;

import com.market.bean.MarketTopic;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminTopicReadVo;
import com.market.exception.ValidationException;
import com.market.service.admin.AdminTopicService;
import com.market.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author fan
 * @Description:
 * @since 2022/12/29 20:42
 */
@RestController
@RequestMapping("admin/topic")
public class AdminTopicController {

    @Autowired
    AdminTopicService topicService;

    @GetMapping("list")
    public BaseRespVo list(BasePageInfo info, String title, String subtitle) {
        CommonData<MarketTopic> data = topicService.list(info, title, subtitle);
        return BaseRespVo.ok(data);
    }

    @PostMapping("create")
    public BaseRespVo create(@RequestBody @Validated MarketTopic marketTopic, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        MarketTopic data = topicService.create(marketTopic);
        return BaseRespVo.ok(data);
    }

    @GetMapping("read")
    public BaseRespVo read(Integer id) {
        AdminTopicReadVo data = topicService.read(id);
        return BaseRespVo.ok(data);
    }

    @PostMapping("update")
    public BaseRespVo update(@RequestBody @Validated MarketTopic marketTopic, BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        MarketTopic data = topicService.update(marketTopic);
        return BaseRespVo.ok(data);
    }

    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketTopic marketTopic) {
        topicService.delete(marketTopic);
        return BaseRespVo.ok();
    }

    @PostMapping("batch-delete")
    public BaseRespVo batchDelete(@RequestBody Map map) {
        List<Integer> ids = (List<Integer>) map.get("ids");
        topicService.batchDelete(ids);
        return BaseRespVo.ok();
    }
}
